package de.tewdreyer.android.erc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ReadDeviceIdentifier extends Activity {

	/**
	 * @see android.view.View.OnClickListener
	 */
	private class ScanOnClickListener implements OnClickListener {
		/**
		 * start barcode scanner activity
		 * @param view view that has been clicked
		 * @see android.view.View.OnClickListener#onClick(View)
		 */
		@Override
        public void onClick(View view) {
            Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent.setPackage("com.google.zxing.client.android");
            intent.putExtra("SCAN_MODE", "ONE_D_MODE");
            startActivityForResult(intent, SCAN_BARCODE);
        }
	}
	
	/**
	 * @see android.view.View.OnClickListener
	 */
	private class OkOnClickListener implements OnClickListener {
		/**
		 * store device identifier in intent
		 * @param view view that has been clicked
		 * @see android.view.View.OnClickListener#onClick(View)
		 */
		@Override
        public void onClick(View view) {
            if(edTxtDeviceId.getText().toString().length() > 0){
                Intent i = new Intent();
                i.putExtra("deviceidentifier", edTxtDeviceId.getText().toString());
                setResult(RESULT_OK,i);
                finish();
            }
        }
	}
	
    /**
     * reference to visual objects
     */
    private Button btnScan;
    private Button btnOk;
    private EditText edTxtDeviceId;
    
    /**
     * number identifying the barcode scanner intent
     */
    private final int SCAN_BARCODE = 0;

    /**
     * initialize all fields and set listener
     * @param inState activity's previous state 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    public void onCreate(Bundle inState) {
        super.onCreate(inState);

        setContentView(R.layout.read_dev_id);

        btnScan = (Button)findViewById(R.id.btnScan);
        btnOk = (Button)findViewById(R.id.btnOk);
        edTxtDeviceId = (EditText)findViewById(R.id.edtTxtDvcId);
        
        // set extern barcode scanner activity to button
        btnScan.setOnClickListener(new ScanOnClickListener());

        // set 'sending identifier' intent to button
        btnOk.setOnClickListener(new OkOnClickListener());
    }

    /**
     * handle returned barcode
     * @param requestCode send on activity start
     * @param resultCode  returned by called activity
     * @param result      intent containing sent data
     * @see android.app.Activity#onActivityResult(int,int,android.content.Intent)
     */
    public void onActivityResult(int requestCode, int resultCode, Intent result) {
    	switch(requestCode){
    	case SCAN_BARCODE:
    		if (resultCode == RESULT_OK) {
    			String scan = result.getStringExtra("SCAN_RESULT");
    			edTxtDeviceId.setText(scan);
    		}
    		break;
    	}
    }
}